package SVN::TeamTools::Store::DataIndex;
use parent 'SVN::TeamTools::Index::Index';

use warnings;
use strict;
use Carp;
use Error qw(:try);

use Sys::Hostname qw( hostname );

use SVN::TeamTools::Store::Config;
use SVN::TeamTools::Store::Repo;

use Data::Dumper;

my $conf;
my $logger;
BEGIN { $conf = SVN::TeamTools::Store::Config->new(); $logger = $conf->{logger}; }

sub hasAction {
        shift;
        my %args        = @_;
        my $action      = $args{action};
        return 0;
}



# #########################################################################################################
#
# Lucy functions
#

# Constructor:
#   - create (0/1)
sub new {
	my $class	= shift;
	my %args	= @_;
	my $mode	= $args{mode};
	my $create	= $args{create};

	my $self = $class->SUPER::new (path => '/u03/data/index/', mode=>$mode, schema=>_getSchema(), create=>$create);

	$self->{_repo} = SVN::TeamTools::Store::Repo->new ();

	bless  $self, $class;

	return $self;
}

sub addDoc {
	my $self	= shift;
	my %args	= @_;

# print Dumper(%args);
	try {
		$self->getWriter()->add_doc (Lucy::Document::Doc->new(fields => {
			schema      => $args{schema},
        	table       => $args{table},
        	column      => $args{column},
        	datatype    => $args{datatype},
        	key         => $args{key},
        	contentraw  => $args{content},
            contentproc => $args{content},
		}));
        } otherwise {
                my $exc = shift;
                croak "Error writing record, error: $exc";
        };
}

###################################################################################################
### Create Lucy Index
#
# Static method
sub _getSchema {
	my $schema = Lucy::Plan::Schema->new;
	my $rawtype = Lucy::Plan::StringType->new(stored=>0);

	$schema->spec_field( name => 'schema', 		type => $rawtype );	# Schemaname
	$schema->spec_field( name => 'table', 		type => $rawtype );	# Tablename
	$schema->spec_field( name => 'column', 		type => $rawtype );	# Column name
	$schema->spec_field( name => 'datatype',    type => $rawtype );	# Column data type
	$schema->spec_field( name => 'key', 		type => $rawtype );	# rowid
	$schema->spec_field( name => 'contentraw',	type => $rawtype );	# doc, link
	$schema->spec_field( name => 'contentproc', type => Lucy::Plan::FullTextType->new(
				analyzer => Lucy::Analysis::PolyAnalyzer->new( language => 'en'),
				stored => 0,
				highlightable => 1,
				boost => 1.5)); # rev

	return $schema;
}
1;
